<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {

		function __construct()
	{
		parent::__construct();
		$this->load->library('ion_auth');
		$this->load->library('session');
		$this->load->library('form_validation');
		$this->load->database();
		$this->load->helper(array('url','language'));
    $this->lang->load('general');
	}

  function index()
  {

    $this->load->model(array('selections_model','riders_model','points_model','stages_model', 'users_model'));
    $this->data['number_of_users']        = $this->users_model->get_number_of_users();
    $this->data['top_10']                 = $this->points_model->get_top_10_overall(); // to show top 10 users
    $this->data['last_stage']             = $this->stages_model->get_last_calculated(); // To show last calculated stage
    $this->data['points_stage']           = explode(',', $this->config_model->item('points_stage'));
    $this->data['points_stage_yellow']    = explode(',', $this->config_model->item('points_stage_yellow'));
    $this->data['points_stage_green']     = explode(',', $this->config_model->item('points_stage_green'));
    $this->data['points_stage_mountain']  = explode(',', $this->config_model->item('points_stage_mountain'));
    if (is_object($this->data['last_stage']))
    {
      $this->data['top_10_last_stage']      = $this->points_model->get_top_10($this->data['last_stage']->stage_id);
    }
    else
    {
      $this->data['top_10_last_stage'] = false;
    }

    if ($this->ion_auth->logged_in())
    {
      $this->data['user'] = $this->ion_auth->user()->row();
      $this->data['selection'] = $this->selections_model->get_by_user($this->data['user']->user_id);
      $this->data['replacements'] = $this->selections_model->get_replacements_by_user($this->data['user']->user_id);
      if (is_object($this->data['last_stage']))
      {
        $this->data['user_points'] = $this->points_model->get_by_user_id_stage_id($this->data['user']->user_id, $this->data['last_stage']->stage_id);
      }
    else
    {
      $this->data['user_points'] = false;
    }

      foreach($this->data['selection'] as $rider)
      {
        if(is_object($rider))
        {
          $this->data['selection_rider_ids'][] = $rider->rider_id; // to check results easier
        }
      }
    }
    else 
    {
      $this->data['identity'] = array('name' => 'identity',
                                      'id' => 'identity',
                                      'type' => 'text',
                                      'value' => $this->form_validation->set_value('identity'),
                                      'class' => 'input-small',
                                      'placeholder' => lang('email')
                                      );
      $this->data['password'] = array('name' => 'password',
                                      'id' => 'password',
                                      'type' => 'password',
                                      'class' => 'input-small',
                                      'placeholder' => lang('password')                                      );
    }

    $this->data['title'] = "Home";
    $this->data['view'] = 'home';
    
    $this->load->view('template', $this->data);

  }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */